﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AIStudio.Wpf.Verify
{

    /// <summary>
    /// TextClickVerify.xaml 的交互逻辑
    /// </summary>
    public partial class TextClickVerify : UserControl
    {
        public TextClickVerify()
        {
            InitializeComponent();

            this.Loaded += TextClickVerify_Loaded; ;
            myCanvas.MouseLeftButtonDown += MyCanvas_MouseLeftButtonDown;
            btnReset.Click += BtnReset_Click;
        }

        private void BtnReset_Click(object sender, RoutedEventArgs e)
        {
            Restart();
        }


        private void MyCanvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (btnReset.Visibility == Visibility.Visible)
            {
                Restart();
                return;
            }

            var position = e.GetPosition(myCanvas);
            AddPath(clicklength - strs.Count + 1, position.X, position.Y);

            if (e.OriginalSource.GetType() == typeof(Grid))
            {
                Grid grid = (Grid)e.OriginalSource;
                if (grid.Tag.ToString() == strs.FirstOrDefault())
                {                 
                    strs.RemoveAt(0);
                    if (strs.Count == 0)
                    {
                        Result = true;           
                        RaiseResultChanged(Result);
                        txtInfo.Visibility = Visibility.Collapsed;
                        btnReset.Visibility = Visibility.Visible;
                        btnReset.Content = "验证成功";
                        btnReset.Background = Brushes.Green;
                    }
                }
                else
                {
                   
                    RaiseResultChanged(Result);
                    txtInfo.Visibility = Visibility.Collapsed;
                    btnReset.Visibility = Visibility.Visible;
                    btnReset.Content = "验证失败，请重试";
                    btnReset.Background = Brushes.Red;
                }
            }
            else
            {
                RaiseResultChanged(Result);
                txtInfo.Visibility = Visibility.Collapsed;
                btnReset.Visibility = Visibility.Visible;
                btnReset.Content = "验证失败，请重试";
                btnReset.Background = Brushes.Red;
            }
        }

        public bool Result
        {
            get { return (bool)GetValue(ResultProperty); }
            set { SetValue(ResultProperty, value); }
        }

        public static readonly DependencyProperty ResultProperty =
            DependencyProperty.Register("Result", typeof(bool), typeof(TextClickVerify), new PropertyMetadata(false));

        public string ImageUri
        {
            get { return (string)GetValue(ImageUriProperty); }
            set { SetValue(ImageUriProperty, value); }
        }

        public static readonly DependencyProperty ImageUriProperty =
            DependencyProperty.Register("ImageUri", typeof(string), typeof(TextClickVerify), new PropertyMetadata(null));

        #region Routed Event
        public static readonly RoutedEvent ResultChangedEvent = EventManager.RegisterRoutedEvent("ResultChanged", RoutingStrategy.Bubble, typeof(EventHandler), typeof(TextClickVerify));
        public event EventHandler ResultChanged
        {
            add { AddHandler(ResultChangedEvent, value); }
            remove { RemoveHandler(ResultChangedEvent, value); }
        }
        void RaiseResultChanged(bool result)
        {
            var arg = new RoutedEventArgs(ResultChangedEvent, result);
            RaiseEvent(arg);
        }
        #endregion

        private void TextClickVerify_Loaded(object sender, RoutedEventArgs e)
        {
            //Restart();
        }

        protected override void OnRender(DrawingContext drawingContext)
        {
            Restart();
        }

        private List<string> strs;
        private int clicklength;
        private void Restart()
        {
            if (!myCanvas.IsVisible)
                return;

            Result = false;

            Random ran = new Random();

            BitmapImage image = GetBitmapImage();
            SetBackground(image);

            //获取GB2312编码页（表） 
            Encoding gb = Encoding.GetEncoding("gb2312");
            string tooltip = string.Empty;
            if (IsEmoji)
            {
                var emoji = EmojiText.Emoji.Value.OrderBy(p => ran.NextDouble()).Take(4).ToList();

                strs = new List<string>();
                strs.Add(emoji[0]);
                strs.Add(emoji[1]);
                strs.Add(emoji[2]);
                strs.Add(emoji[3]);
                clicklength = 3;      
            }
            else if (IsIdioms)
            {
                var idioms = IdiomsText.Idioms.Value;
                var idiomkey = idioms.Keys.ToArray()[ran.Next(0, idioms.Count)];
                tooltip = idioms[idiomkey];
                clicklength = idiomkey.Length;

                strs = new List<string>();
                for(int i = 0; i < clicklength; i ++)
                {
                    strs.Add(idiomkey.Substring(i, 1));
                }       
            }
            else
            {
                //调用函数产生4个随机中文汉字编码 
                object[] bytes = ChineseCode.CreateRegionCode(4);

                //根据汉字编码的字节数组解码出中文汉字 
                strs = new List<string>();
                strs.Add(gb.GetString((byte[])Convert.ChangeType(bytes[0], typeof(byte[]))));
                strs.Add(gb.GetString((byte[])Convert.ChangeType(bytes[1], typeof(byte[]))));
                strs.Add(gb.GetString((byte[])Convert.ChangeType(bytes[2], typeof(byte[]))));
                strs.Add(gb.GetString((byte[])Convert.ChangeType(bytes[3], typeof(byte[]))));
                clicklength = 3;
            }
     

            int width = (int)(myCanvas.ActualWidth - 30);
            int height = (int)(myCanvas.ActualHeight - 40);
            var brush = Application.Current.FindResource("MahApps.Brushes.Accent") as Brush;

            var clickstrs = strs.OrderBy(p => ran.NextDouble()).ToArray();
            myCanvas.Children.Clear();
            for (int i = 0; i < strs.Count; i++)
            {
                AddChild(clickstrs[i], i, strs.Count, brush, width, height, ran);
            }
            strs = strs.Take(clicklength).ToList();

            txtInfo.Visibility = Visibility.Visible;
            if (clicklength == 3)
            {
                txtInfo.Text = $"请依次点击\"{strs[0]}\"\"{strs[1]}\"\"{strs[2]}\"";
                txtInfo.ToolTip = txtInfo.Text;
            }
            else
            {
                txtInfo.Text = "请按语序依次点击文字";
                txtInfo.ToolTip = $"\"{string.Join("", strs)}\"{tooltip}";
            }
            btnReset.Visibility = Visibility.Collapsed;
            btnReset.Background = Brushes.Transparent;
        }
     
        public void AddChild(string str, int index, int totalindex, Brush brush, int width, int height, Random ran)
        {
            Grid grid = new Grid();
            grid.Tag = str;
            OutlineText outlinetext = new OutlineText()
            {
                FontSize = 30,
                Text = str,
                FontWeight = FontWeights.Bold,
                Fill = new SolidColorBrush (Color.FromRgb(Convert.ToByte(ran.Next(0, 255)), Convert.ToByte(ran.Next(0, 255)), Convert.ToByte(ran.Next(0, 255)))),//brush,
                IsHitTestVisible = false,
            };
            grid.Children.Add(outlinetext);

            SetLeft(grid, ran.Next((int)(width * index / totalindex)  , (int)(width * (index + 1)/ totalindex)));
            SetTop(grid, ran.Next(0, (int)height));
            RotateTransform rtf = new RotateTransform(ran.Next(0, 360), 15, 20);
            grid.RenderTransform = rtf;
          
            grid.Background = new SolidColorBrush(Colors.Transparent);
            myCanvas.Children.Add(grid);
        }

        private void AddPath(int number, double left, double top)
        {
            Grid grid = new Grid();

            Path path = new Path();
            path.Fill = Application.Current.FindResource("MahApps.Brushes.Accent") as Brush;// Application.Current.FindResource("MahApps.Brushes.ThemeForeground") as Brush;
            path.Stroke = Application.Current.FindResource("MahApps.Brushes.ThemeBackground") as Brush;
            string sData = "M12,2A7,7 0 0,0 5,9C5,14.25 12,22 12,22C12,22 19,14.25 19,9A7,7 0 0,0 12,2Z";
            var converter = TypeDescriptor.GetConverter(typeof(Geometry));
            path.Data = (Geometry)converter.ConvertFrom(sData);
            path.Height = 40;
            path.Width = 30;
            path.StrokeThickness = 2;
            path.Stretch = Stretch.Fill;
            path.HorizontalAlignment = HorizontalAlignment.Center;
            path.VerticalAlignment = VerticalAlignment.Center;

            grid.Children.Add(path);

            TextBlock text = new TextBlock();
            text.Text = number.ToString();
            text.Foreground = Application.Current.FindResource("MahApps.Brushes.ThemeBackground") as Brush;
            text.HorizontalAlignment = HorizontalAlignment.Center;
            text.VerticalAlignment = VerticalAlignment.Center;
            text.Margin = new Thickness(0, 0, 0, 2);
            grid.Children.Add(text);

            myCanvas.Children.Add(grid);
            SetLeft(grid, left - path.Width / 2);
            SetTop(grid, top - path.Height);
        }

        private BitmapImage GetBitmapImage()
        {           
            Random ran = new Random();
            int value = ran.Next(1, 3);

            // Create source.
            BitmapImage image = new BitmapImage();
            // BitmapImage.UriSource must be in a BeginInit/EndInit block.
            image.BeginInit();
            image.UriSource = new Uri(ImageUri ?? $"pack://application:,,,/AIStudio.Wpf.Verify;component/Resources/{value}.jpg");
            image.DecodePixelWidth = (int)myCanvas.ActualWidth;
            image.DecodePixelHeight = (int)myCanvas.ActualHeight;
            image.EndInit();

            return image;
        }

        private void SetBackground(BitmapImage image)
        {
            ImageBrush ib = new ImageBrush();
            ib.ImageSource = image;

            myCanvas.Background = ib;
        }
       

        private void SetVerCenter(FrameworkElement element)
        {
            double top = (myCanvas.ActualHeight - element.ActualHeight) / 2;
            Canvas.SetTop(element, top);
        }

        private void SetLeft(FrameworkElement element, double left)
        {
            Canvas.SetLeft(element, left);
        }

        private void SetTop(FrameworkElement element, double top)
        {
            Canvas.SetTop(element, top);
        }

        public bool IsEmoji
        {
            get { return (bool)GetValue(IsEmojiProperty); }
            set { SetValue(IsEmojiProperty, value); }
        }

        public static readonly DependencyProperty IsEmojiProperty =
            DependencyProperty.Register("IsEmoji", typeof(bool), typeof(TextClickVerify), new PropertyMetadata(false));

        public bool IsIdioms
        {
            get { return (bool)GetValue(IsIdiomsProperty); }
            set { SetValue(IsIdiomsProperty, value); }
        }

        public static readonly DependencyProperty IsIdiomsProperty =
            DependencyProperty.Register("IsIdioms", typeof(bool), typeof(TextClickVerify), new PropertyMetadata(false));

    }
}
